//
//  HeaderView.m
//  News
//
//  Created by xalo on 16/3/9.
//  Copyright © 2016年 News. All rights reserved.
//

#import "HeaderView.h"
#import "GroupsModel.h"

@interface HeaderView ()

@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *title;
@property (nonatomic,strong) UILabel *number;
@property (nonatomic,strong) UIButton *button;
@end

@implementation HeaderView

// 创建表头
- (HeaderView *)headerViewWithTableView:(UITableView *)tableView {
    
    static NSString *identifier = @"headerView";
    
    // tableView方法,从重用队列中取出表头
    HeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (headerView) {
        headerView = [[HeaderView alloc] initWithReuseIdentifier:identifier];
    }
    return headerView;
}

//- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
//{
//    self = [super initWithReuseIdentifier:reuseIdentifier];
//    if (self) {
//        
//        UIView * _line = [[UIView alloc] initWithFrame:CGRectMake(0, 44-0.5, [UIScreen mainScreen].bounds.size.width, 0.5)];
//        _line.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
//        [self.contentView addSubview:_line];
//        
//    }
//    return self;
//}

- (void)setGroup:(GroupsModel *)group {
    
    _group = group;
    if (_group) {
        self.title.text = group.title;
        self.number.text = [NSString stringWithFormat:@"%ld",group.newsModelArray.count];
        [self setArrowImageViewWithIfUnfold:self.group.isOpen];
    }
}
/**
 *   设置图片箭头旋转
 */
-(void)setArrowImageViewWithIfUnfold:(BOOL)unfold
{
    double degree;
    if(unfold){
        degree = M_PI;
    } else {
        degree = 0;
    }
    
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        self.imageView.layer.transform = CATransform3DMakeRotation(degree, 0, 0, 1);
    } completion:NULL];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(10, (self.frame.size.height-9)/2.0, 14, 9);
    self.title.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame)+10, 0, 100, self.frame.size.height);
    self.button.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.number.frame = CGRectMake(self.frame.size.width-50, 0, 40, self.frame.size.height);
    
}

- (UILabel *)title{
    if (!_title) {
        _title = [[UILabel alloc]init];
        _title.font = [UIFont systemFontOfSize:14.0];
        _title.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_title];
    }
    return _title;
}

- (UILabel *)number{
    if (!_number) {
        _number = [[UILabel alloc]init];
        _number.font = [UIFont systemFontOfSize:12.0];
        _number.textAlignment = NSTextAlignmentRight;
        _number.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_number];
    }
    return _number;
}

- (UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_button];
    }
    return _button;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"account_arrows"];
        [self.contentView addSubview:_imageView];
    }
    return _imageView;
}

-(void)buttonClick:(UIButton *)sender
{
    self.group.isOpen =! self.group.isOpen;
    
    if ([self.delegate respondsToSelector:@selector(headerView:withButton:)]) {
        [self.delegate headerView:self withButton:sender];
    }
}




@end
